package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @RequestMapping("login")
    public void login(HttpServletResponse response, HttpServletRequest request, String username, String password) throws ServletException, IOException {
        try{
            employeeService.login(username,password);
            response.sendRedirect("/department/list.do");
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("errorMsg",e.getMessage());
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    @RequestMapping("logout")
    public String logout(){
        UserContext.getSession().invalidate();
        return "redirect:/login.jsp";
    }
}
